﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainScript
{
    public class ConsoleLogger
    {
        List<string> logs = new List<string>();
        private int indent;

        public int Indent
        {
            get { return indent; }
            set
            {
                if (value >= 0)
                    indent = value;
                tabs = "";
                for (int i = 0; i < indent; i++)
                {
                    tabs += '\t';
                }
            }
        }

        string tabs = "";

        readonly ConsoleColor warning_color   = ConsoleColor.Yellow;
        readonly ConsoleColor error_color     = ConsoleColor.Red;
        readonly ConsoleColor directory_color = ConsoleColor.Gray;
        readonly ConsoleColor status_color    = ConsoleColor.White;
        readonly ConsoleColor winColor = ConsoleColor.Green;
        readonly ConsoleColor smartass_color = ConsoleColor.Blue;
        ConsoleColor default_color;
        public LogType Severity { get; set; }
        public ConsoleLogger()
        {
            default_color = Console.ForegroundColor;
            Severity = LogType.Directory;
        }

        public ConsoleLogger(LogType severity)
        {
            default_color = Console.ForegroundColor;
            Severity = severity;
        }

        public void Log(string s)
        {
            ResetColor();
            Console.WriteLine(s);
        }

        public void Log(string s, LogType type)
        {
            if (type < Severity)
                return;
            switch (type)
            {
                case LogType.Directory:
                    Log(string.Format("{0}{1}", tabs, s), directory_color);
                    break;
                case LogType.Warning:
                    Log(string.Format("{0}{1}", tabs, s), warning_color);
                    logs.Add(string.Format("{0}## Warning: {1}", tabs, s));
                    break;
                case LogType.Error:
                    Log(string.Format("{0}{1}", tabs, s), error_color);
                    logs.Add(string.Format("{0}### Error: {1}", tabs, s));
                    break;
                case LogType.Status:
                    Log(string.Format("{0}{1}", tabs, s), status_color);
                    logs.Add(string.Format("{0}{1}", tabs, s));
                    break;
                case LogType.Win:
                    Log(string.Format("{0}{1}", tabs, s), winColor);
                    logs.Add(string.Format("{0}# Win: {1}", tabs, s));
                    break;
                case LogType.Smartass:
                    Log(string.Format("{0}{1}", tabs, s), smartass_color);
                    logs.Add(string.Format("{0}# Smartass: {1}", tabs, s));
                    break;
                default:
                    Log(string.Format("{0}{1}", tabs, s), default_color);
                    break;
            }
        }

        public void Log(LogType type, string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, type);
        }

        private void Log(string s, ConsoleColor color)
        {
            ChangeColor(color);
            Console.WriteLine(s);
            ResetColor();
        }

        public void SaveLogs(string name)
        {
            System.IO.File.WriteAllLines(name, logs.ToArray());
        }

        public void ReportWarn(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Warning);
        }

        public void ReportError(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Error);
        }

        public void ReportStatus(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Status);
        }

        public void ReportDirectory(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Directory);
        }

        public void ReportWin(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Win);
        }

        public void ReportSmartass(string format, params object[] args)
        {
            var s = string.Format(format, args);
            Log(s, LogType.Smartass);
        }

        void ChangeColor(ConsoleColor new_color)
        {
            Console.ForegroundColor = new_color;
        }

        void ResetColor()
        {
            Console.ForegroundColor = default_color;
        }
    }

    public enum LogType
    {
        Directory = 0,
        Warning = 1,
        Error = 2,
        Status = 3,
        Win = 4,
        Smartass = 5
    }
}
